import java.util.List;

/*
 * @author zzr
 * @date: 2025/10/12  14:50
 * @description: 给一个点链表的头结点 head 将链表进行反转，返回反转后的链表
 */
public class Demo4 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }

        ListNode cur = head.next;
        // 头节点反转后一定成为了尾节点
        head.next = null;

        while (cur != null) {
            // 先把 cur 后面接待你存起来，然后 进行反转
            // 反转完成后，cur 成为了新的 head 节点 cur 再向后移动
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
}
